package com.LeeCode;

import java.util.Arrays;

/**
 * 有序数组中差绝对值之和
 */

public class Code1685 {
    public static void main(String[] args) {
        int[] nums = {1, 4, 6, 8, 10};
        System.out.println(Arrays.toString(new Code1685().getSumAbsoluteDifferences(nums)));
    }

    public int[] getSumAbsoluteDifferences(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n], preSum = new int[n + 1];
        for (int i = 0; i < n; i++) {
            preSum[i + 1] = preSum[i] + nums[i];
        }
        for (int i = 0; i < n; i++) {
            ans[i] = (2 * i + 2 - n) * nums[i] + preSum[n] - 2 * preSum[i + 1];
        }
        return ans;
    }
}
